//
// Created by yhy on 17-7-8.
//

#include "MyReactor.h"

using namespace Poco::Net;



void MyReactor::onTimeout() {
    _needCheckTimeout();
    SocketReactor::onTimeout();
}

void MyReactor::onIdle() {
    _needCheckTimeout();
    SocketReactor::onIdle();
}

void MyReactor::onBusy() {
    _needCheckTimeout();
    SocketReactor::onBusy();
}


bool MyReactor::_needCheckTimeout() {
    //获取当前时间
    time_t timestamp = ::time(nullptr);

    if( timestamp - lastCheckTime > notifyAllSingletonCheckTimeoutNotification ){
        nc.postNotification( new SingletonCheckTimeoutNotification);
        lastCheckTime = timestamp;
    }
//    std::cout << "lastCheckTime:" << lastCheckTime << "    current:" << timestamp << std::endl;
    return false;
}

int MyReactor::getNotifyAllSingletonCheckTimeoutNotification() const {
    return notifyAllSingletonCheckTimeoutNotification;
}

void MyReactor::setNotifyAllSingletonCheckTimeoutNotification(int notifyAllSingletonCheckTimeoutNotification) {
    MyReactor::notifyAllSingletonCheckTimeoutNotification = notifyAllSingletonCheckTimeoutNotification;
}

void MyReactor::addTimeoutHandler(const Poco::AbstractObserver &observer) {
    nc.addObserver(observer);
}

void MyReactor::removeTimeoutHandler(const Poco::AbstractObserver &observer) {
    nc.removeObserver(observer);
}

